<?php

namespace App\Admin\Controllers;

use App\Models\Specials;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class SpeciasController extends Controller
{
    use ModelForm;

    public $header_title = '热门专题';
    
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header_title);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header_title);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header_title);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Specials::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->disableExport();

            $grid->column('title','专题名称');
            $grid->image('专题大图')->image(env('APP_URL') . '/uploads/',100,100);
//             $grid->column('image','专题大图')->display(function ($image) {
                
//                 return "<a href='". $image."' target='_blank'>$image</a>";
                
//             });
            $grid->column('url','链接')->display(function ($url) {
                
                return "<a href='". $url ."' target='_blank' title='点击跳转到大图'>$url</a>";
                
            });
            $grid->created_at('上线时间');
            $grid->updated_at('最近更新');
            $grid->disableExport();
            
            $grid->filter(function($filter){
                
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                
                $filter->where(function ($query) {
                    $title = $this->input;
                    
                    $query->whereRaw("`title` like '%{$title}%' AND `status` = 1");
                    
                }, '专题标题');
                
                                
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Specials::class, function (Form $form) {

            
            $form->text('title','专题标题')->help('30个中文字符以内')->rules('required|max:30');
            $form->text('summary', '概要')
                ->attribute('maxlength', '50')
                ->rules('required');
            $form->text('url','专题链接')->help('专题链接,示例 http://www.baidu.com')->rules('required|url');
            $form->image('image','专题大图')
                ->removable()
                ->attribute('accept', 'image/png, image/jpeg, image/jpg')
                ->rules('required|mimes:png,jpeg,jpg')
                ->uniqueName()
                ->help('建议尺寸278*310，不大于2M，支持格式：png、jpeg、jpg');
            $form->display('created_at', '创建时间');
        });
    }
}
